<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="头像路径" prop="userAvatar">
        <el-input
          v-model="queryParams.userAvatar"
          placeholder="请输入头像路径"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="本名字" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入本名字"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="原名字" prop="toName">
        <el-input
          v-model="queryParams.toName"
          placeholder="请输入原名字"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发表日期" prop="date">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.date"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择发表日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="发表内容" prop="content">
        <el-input
          v-model="queryParams.content"
          placeholder="请输入发表内容"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="赞人数" prop="likeNum">
        <el-input
          v-model="queryParams.likeNum"
          placeholder="请输入赞人数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="父级id" prop="ucPId">
        <el-input
          v-model="queryParams.ucPId"
          placeholder="请输入父级id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="回复数" prop="replyNum">
        <el-input
          v-model="queryParams.replyNum"
          placeholder="请输入回复数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['communication:communication:add']"
        >新增</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="communicationList"
      row-key="ucId"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <!--<el-table-column label="头像路径" align="center" prop="userAvatar" />-->
      <el-table-column label="本名字" align="center" prop="userName" />
      <el-table-column label="原名字" align="center" prop="toName" />
      <el-table-column label="发表日期" align="center" prop="date" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.date) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发表内容" align="center" prop="content" />
      <el-table-column label="赞人数" align="center" prop="likeNum" />
      <el-table-column label="父级id" align="center" prop="ucPId" />
      <el-table-column label="回复数" align="center" prop="replyNum" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['communication:communication:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['communication:communication:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改用户交流对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="原名字" prop="toName">
          <el-input v-model="form.toName" placeholder="请输入原名字" />
        </el-form-item>
        <el-form-item label="发表内容" prop="content">
          <el-input v-model="form.content" placeholder="请输入发表内容" />
        </el-form-item>
        <el-form-item label="赞人数" prop="likeNum">
          <el-input v-model="form.likeNum" placeholder="请输入赞人数" />
        </el-form-item>
        <el-form-item label="父级id" prop="ucPId">
          <treeselect v-model="form.ucPId" :options="communicationOptions" :normalizer="normalizer" placeholder="请选择父级id" />
        </el-form-item>
        <el-form-item label="回复数" prop="replyNum">
          <el-input v-model="form.replyNum" placeholder="请输入回复数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCommunication, getCommunication, delCommunication, addCommunication, updateCommunication, exportCommunication } from "@/api/communication/communication";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Communication",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 用户交流表格数据
      communicationList: [],
      // 用户交流树选项
      communicationOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        userAvatar: undefined,
        userName: undefined,
        toName: undefined,
        date: undefined,
        content: undefined,
        likeNum: undefined,
        ucPId: undefined,
        replyNum: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户交流列表 */
    getList() {
      this.loading = true;
      listCommunication(this.queryParams).then(response => {
        this.communicationList = this.handleTree(response.data, "ucId", "ucPId");
        this.loading = false;
      });
    },
    /** 转换用户交流数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.ucId,
        label: node.userName,
        children: node.children
      };
    },
	/** 查询部门下拉树结构 */
    getTreeselect() {
      listCommunication().then(response => {
        this.communicationOptions = [];
        const data = { ucId: 0, userName: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "ucId", "ucPId");
        this.communicationOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ucId: undefined,
        userAvatar: undefined,
        userName: undefined,
        toName: undefined,
        date: undefined,
        content: undefined,
        likeNum: undefined,
        ucPId: undefined,
        replyNum: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
	  this.getTreeselect();
      this.open = true;
      this.title = "添加用户交流";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
	  this.getTreeselect();
      if (row != undefined) {
        this.form.ucPId = row.ucId;
      }
      getCommunication(row.ucId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户交流";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.ucId != undefined) {
            updateCommunication(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addCommunication(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除用户交流编号为"' + row.ucId + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCommunication(row.ucId);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    }
  }
};
</script>